package com.goods.business.service.imp;

import com.goods.business.mapper.*;
import com.goods.business.service.InStockService;
import com.goods.common.error.SystemException;
import com.goods.common.model.business.*;
import com.goods.common.vo.business.InStockItemVO;
import com.goods.common.vo.business.InStockVO;
import com.goods.common.vo.system.UserInfoVO;
import com.goods.system.service.UserService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;
import java.util.stream.Collectors;

@Service
public class InStockServiceImpl implements InStockService {
    @Resource
    private InStockMapper inStockMapper;
    @Resource
    private SupplierMapper supplierMapper;
    @Resource
    private InStockInfoMapper inStockInfoMapper;
    @Resource
    private ProductStockMapper productStockMapper;
    @Resource
    private ProductMapper productMapper;
    @Resource
    private UserService userService;

    @Override
    public List<InStockVO> findInStockList(InStockVO inStockVO) {

        List<InStockVO> list = inStockMapper.selectByInStockVo(inStockVO);
        list.stream().map(inStockVO1 -> {
            inStockVO1.setSupplierName(inStockVO1.getName());
            return inStockVO1;
        }).collect(Collectors.toList());
        return list;
    }

    @Override
    public List<InStockItemVO> findItemVOS(Long id) {
        //List<InStockItemVO> list = inStockMapper.selectItem(id);
        return inStockMapper.selectItem(id);
    }

    @Override
    public InStockVO findSupplierVO(Long id) {

        return inStockMapper.selectSupplier(id);
    }

    @Override
    public Integer remove(Long id) {
        InStock inStock = new InStock();
        inStock.setId(id);
        inStock.setStatus(1);
        return inStockMapper.updateByPrimaryKeySelective(inStock);
    }

    @Override
    public Integer back(Long id) {
        InStock inStock = new InStock();
        inStock.setId(id);
        inStock.setStatus(0);
        return inStockMapper.updateByPrimaryKeySelective(inStock);
    }


    @Override
    public Integer delete(Long id) {

        InStock inStock = inStockMapper.selectByPrimaryKey(id);
        InStockInfo inStockInfo = new InStockInfo();
        inStockInfo.setInNum(inStock.getInNum());
        inStockInfoMapper.delete(inStockInfo);
        return inStockMapper.deleteByPrimaryKey(id);
    }

    @Override
    public Integer addIntoStock(InStockVO inStockVO) {
        List<Object> products = inStockVO.getProducts();
        Long supplierId = inStockVO.getSupplierId();
        Supplier supplier = new Supplier();
        String inNum = UUID.randomUUID().toString().replaceAll("-", "");
        Integer num = 0;
        Date date = new Date();
        if (supplierId == null) {
            supplier.setName(inStockVO.getName());
            supplier.setAddress(inStockVO.getAddress());
            supplier.setEmail(inStockVO.getEmail());
            supplier.setPhone(inStockVO.getPhone());
            supplier.setSort(inStockVO.getSort());
            supplier.setContact(inStockVO.getContact());
            supplier.setCreateTime(date);
            supplier.setModifiedTime(date);
            supplierMapper.insert(supplier);
        }

        for (Object product : products) {
            Map map = (HashMap) product;
            String productId = map.get("productId").toString();
            Integer productNumber = Integer.parseInt(map.get("productNumber").toString());
            Product product1 = productMapper.selectByPrimaryKey(productId);
            InStockInfo inStockInfo = new InStockInfo();
            inStockInfo.setInNum(inNum);
            inStockInfo.setPNum(product1.getPNum());
            inStockInfo.setProductNumber(productNumber);
            inStockInfo.setCreateTime(date);
            inStockInfo.setModifiedTime(date);
            inStockInfoMapper.insert(inStockInfo);
            ProductStock productStock = new ProductStock();
            productStock.setPNum(productId);
            //ProductStock productStock1 = productStockMapper.selectOne(productStock);
            //productStock1.setStock(productStock1.getStock() + productNumber);
            //productStockMapper.updateByPrimaryKeySelective(productStock1);
            num = num + productNumber;
        }
        String username = null;
        try {
            UserInfoVO info =  userService.info();
            username = info.getUsername();
        } catch (SystemException e) {
            e.printStackTrace();
        }
        InStock inStock = new InStock();
        inStock.setRemark(inStockVO.getRemark());
        inStock.setType(inStockVO.getType());
        inStock.setInNum(inNum);
        inStock.setStatus(2);
        inStock.setCreateTime(date);
        inStock.setModified(date);
        inStock.setOperator(username);
        inStock.setProductNumber(num);
        inStock.setSupplierId(supplier.getId());
        if (supplierId != null){
            inStock.setSupplierId(supplierId);
        }


        return inStockMapper.insert(inStock);

    }

    @Override
    public Integer publish(Long id) {
        InStock inStock = new InStock();
        inStock.setId(id);
        inStock.setStatus(0);
        List<Map> list = inStockMapper.selectProductStockList(id);
        for (Map map : list) {
            ProductStock productStock = new ProductStock();
            productStock.setId(Long.parseLong(map.get("id").toString()));
            productStock.setPNum(map.get("p_num").toString());
            productStock.setStock(Long.parseLong(map.get("stock").toString()) + Long.parseLong(map.get("product_number").toString()));
            productStockMapper.updateByPrimaryKeySelective(productStock);
        }
        return inStockMapper.updateByPrimaryKeySelective(inStock);
        //return null;
    }
}
